﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SM2TestRunner;
using Repository;

namespace Popups
{
    public partial class ProfileInput : Form
    {
        public static string strAction;

        public ProfileInput()
        {
            InitializeComponent();
        }

        public static DialogResult ShowProfileInput(string action)
        {
            ProfileInput dialog = new ProfileInput();
            strAction = action;
            return dialog.ShowDialog();
        }

        private void ProfileInput_Load(object sender, EventArgs e)
        {
            #region PROFILE
            cbbProfiles.DisplayMember = "Attribute2";
            cbbProfiles.ValueMember = "Attribute1";
            try
            {
                cbbProfiles.DataSource = Repository.ServerHardDataUtil.buildProfilesDataTable();
            }
            catch (Exception ex)
            {
                Utilities.GlobalObject.LOGGER.log("Failed to load ListBox Dashboard.CHART Utilities.GlobalInit.buildProfilesDataTable()");
                Utilities.GlobalObject.LOGGER.log(ex.ToString());
            }
            #endregion

            switch (strAction)
            {
                case "createNewProfile":
                    #region
                    labelTitle.Text = "Create New Profile";
                    groupBox1.Enabled = false;
                    radioButton2.Checked = true;radioButton1.Checked = false;                    
                    break;
                    #endregion

                case "deleteProfile":
                    #region
                    labelTitle.Text = "Delete Profile";
                    tbNewProfileName.Enabled = false;
                    
                    cbbProfiles.Enabled = true;radioButton3.Checked = true;
                    textBoxProfileName.Enabled = false;radioButton4.Checked = false;

                    radioButton2.Checked = true;radioButton1.Checked = false;
                    break;
                    #endregion

                case "openProfile":
                    #region
                    labelTitle.Text = "Load Profile";
                    tbNewProfileName.Enabled = false;

                    cbbProfiles.Enabled = true;radioButton3.Checked = true;
                    textBoxProfileName.Enabled = false;radioButton4.Checked = false;

                    groupBox2.Enabled = false;
                    break;
                    #endregion

                case "renameProfile":
                    #region
                    labelTitle.Text = "Rename Profile";

                    cbbProfiles.Enabled = true;radioButton3.Checked = true;
                    textBoxProfileName.Enabled = false;radioButton4.Checked = false;

                    radioButton2.Checked = true;radioButton1.Checked = false;
                    break;
                    #endregion

                case "copyProfile":
                    #region
                    labelTitle.Text = "Create New Profile By Using 'copy profile'";

                    cbbProfiles.Enabled = true;radioButton3.Checked = true;
                    textBoxProfileName.Enabled = false;radioButton4.Checked = false;
                    
                    radioButton2.Checked = true;radioButton1.Checked = false;
                    break;
                    #endregion

                default:
                    break;
            }
        }

        private void btnDISCARD_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            cbbProfiles.Enabled = false;
            textBoxProfileName.Enabled = true;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            cbbProfiles.Enabled = true;
            textBoxProfileName.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            InputDispatcher.TEST_PARAMETER = new TestParameters();

            switch (strAction)
            {
                case "createNewProfile":
                    #region
                    string strProfileName = tbNewProfileName.Text.Trim();
                    InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName);

                    if (radioButton1.Checked)
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "no");
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "yes");
                        //Update file SERVER_HARDATA.xml
                        ServerHardDataUtil.saveProfileName(strProfileName);
                    }

                    break;
                    #endregion

                case "deleteProfile":
                    #region
                    labelTitle.Text = "Delete Profile";
                    tbNewProfileName.Enabled = false;

                    string strSelectedProfileName;
                    if (textBoxProfileName.Enabled)
                    {
                        strSelectedProfileName = textBoxProfileName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName);
                    }
                    else
                    {
                        strSelectedProfileName = cbbProfiles.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName);
                    }

                    if (radioButton1.Checked)
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "no");
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "yes");
                        //Update file SERVER_HARDATA.xml
                        ServerHardDataUtil.deleteProfileName(strSelectedProfileName);
                    }


                    break;
                    #endregion

                case "openProfile":
                    #region
                    labelTitle.Text = "Load Profile";
                    tbNewProfileName.Enabled = false;
                    groupBox2.Enabled = false;

                    string strSelectedProfileName_Open;
                    if (textBoxProfileName.Enabled)
                    {
                        strSelectedProfileName_Open = textBoxProfileName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName_Open);
                    }
                    else
                    {
                        strSelectedProfileName_Open = cbbProfiles.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName_Open);
                    }

                    break;
                    #endregion

                case "renameProfile":
                    #region
                    labelTitle.Text = "Rename Profile";

                    string strProfileName_Rename = tbNewProfileName.Text.Trim();
                    InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName_Rename);

                    string strSelectedProfileName_Rename;
                    if (textBoxProfileName.Enabled)
                    {
                        strSelectedProfileName_Rename = textBoxProfileName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName_Rename);
                    }
                    else
                    {
                        strSelectedProfileName_Rename = cbbProfiles.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName_Rename);
                    }

                    if (radioButton1.Checked)
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "no");
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "yes");

                        //Update file SERVER_HARDATA.xml
                        ServerHardDataUtil.deleteProfileName(strSelectedProfileName_Rename);
                        ServerHardDataUtil.saveProfileName(strProfileName_Rename);
                    }

                    break;
                    #endregion

                case "copyProfile":
                    #region
                    labelTitle.Text = "Create New Profile By Using 'copy profile'";

                    string strProfileName_Copy = tbNewProfileName.Text.Trim();
                    InputDispatcher.TEST_PARAMETER.add("Profile Name", strProfileName_Copy);

                    string strSelectedProfileName_Copy;
                    if (textBoxProfileName.Enabled)
                    {
                        strSelectedProfileName_Copy = textBoxProfileName.Text.Trim();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName_Copy);
                    }
                    else
                    {
                        strSelectedProfileName_Copy = cbbProfiles.SelectedValue.ToString();
                        InputDispatcher.TEST_PARAMETER.add("Selected Profile Name", strSelectedProfileName_Copy);
                    }

                    if (radioButton1.Checked)
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "no");
                    }
                    else
                    {
                        InputDispatcher.TEST_PARAMETER.add("Confirm", "yes");
                        //Update file SERVER_HARDATA.xml
                        ServerHardDataUtil.saveProfileName(strProfileName_Copy);
                    }

                    break;
                    #endregion

                default:
                    break;
            }

            this.Close();
        }
    }
}
